/**
 * @author ady
 * @version 1.0
 * @date 2021/7/6 14:49
 */
import {getDeptExtra, getDeptExtraSet} from "@/api/api";

export const extra = {
    data() {
        return {
            corpCodeType: "",
            corpCode: "",
            registerDate: "",
            bizScope: "",
            legalMan: "",
            legalManPhone: "",
            address: "",
            shortInfo: "",
            imgList: [],
            name: "",
            money: "",
            startDate: "",
            completeDate: "",
            period: "",
            projectInfo: "",
            unitList: [],
            extraType: "",
            isExtraFlag: false,
            gpsModal: false,
            ads: "",
            marker: null,
            map: "",
            lng: "",
            lat: "",
            adsLat: "",
            adsLng: "",
            options: {
                disabledDate(date) {
                    return date > Date.now();
                }
            },
            fileList:[],
            postList:[],
            title:"",
        }
    },
    methods: {
        getStartDate(value) {
            this.startDate = value;
        },
        getCompleteDate(value) {
            this.completeDate = value;
        },
        getRegisterDate(value) {
            this.registerDate = value;
        },
        getGPS() {
            var _that = this;
            _that.gpsModal = true;
            _that.ads = "";
            _that.map = new AMap.Map("container", {
                zoom: 8,
            });
            AMap.event.addListener(_that.map, "click", function (e) {
                if (_that.marker != null) {
                    _that.map.remove(_that.marker);
                }
                _that.marker = new AMap.Marker({
                    icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png",
                    position: [e.lnglat.lng, e.lnglat.lat],
                    offset: new AMap.Pixel(-20, -40)
                });
                _that.marker.setMap(_that.map);
                _that.lng = e.lnglat.lng;
                _that.lat = e.lnglat.lat;
                AMap.service("AMap.Geocoder", function () {
                    //回调函数
                    //实例化Geocoder
                    var geocoder = new AMap.Geocoder({
                        city: "" //城市，默认:“全国”
                    });
                    var lnglatXY = [e.lnglat.getLng(), e.lnglat.getLat()]; //地图上所标点的坐标
                    geocoder.getAddress(lnglatXY, function (status, result) {
                        if (status === "complete" && result.info === "OK") {
                            //获得了有效的地址信息:
                            //即，result.regeocode.formattedAddress
                            var city = result.regeocode.formattedAddress;
                        } else {
                            var city = "暂无位置信息";
                            //获取地址失败
                        }
                        _that.ads = city;
                    });
                });
            });
            if (_that.lng != "") {
                _that.marker = new AMap.Marker({
                    icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png",
                    position: [_that.lng, _that.lat],
                    offset: new AMap.Pixel(-20, -40)
                });
                _that.marker.setMap(_that.map);
            } else {
                _that.marker = new AMap.Marker({});
            }
        },
        submitGPS() {
            if (this.lng == "") {
                this.$Modal.warning({
                    title: "提示",
                    content: "请选择项目位置"
                });
                return
            }
            this.adsLat = this.lat;
            this.adsLng = this.lng;
            this.address = this.ads;
            this.gpsModal = false;
        },
        searchAddress() {
            if (this.ads == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请输入地址"
                });
                return;
            }
            var _that = this;
            if (_that.marker != null) {
                _that.map.remove(_that.marker);
            }
            var geocoder = new AMap.Geocoder({
                city: "", //城市设为北京，默认：“全国”
            });
            _that.marker = new AMap.Marker();
            geocoder.getLocation(_that.ads, function (status, result) {
                if (status === 'complete' && result.geocodes.length) {
                    var lnglat = result.geocodes[0].location
                    _that.lng = lnglat.lng;
                    _that.lat = lnglat.lat;
                    _that.address = _that.ads;
                    _that.marker.setPosition(lnglat);
                    _that.map.add(_that.marker);
                    _that.map.setFitView(_that.marker);
                } else {
                    _that.$Message.warning('根据地址查询位置失败');
                    // log.error('根据地址查询位置失败');
                }
            });
        },
        addUnit() {
            let obj = {}
            this.unitList.push(obj)
        },
        remove(index, type) {
            this.unitList.splice(index, 1);
        },
        getExtra(id) {
            this.imgList = [];
            let params = {
                deptId: id
            }
            getDeptExtra(params).then(res => {
                if (res.code == 200) {
                    this.fileList =res.data.imgList || [];
                    this.corpCodeType = res.data.corpCodeType || "";
                    this.corpCode = res.data.corpCode || "";
                    this.registerDate = res.data.registerDate || "";
                    this.bizScope = res.data.bizScope || "";
                    this.legalMan = res.data.legalMan || "";
                    this.legalManPhone = res.data.legalManPhone || "";
                    this.address = res.data.address || "";
                    this.shortInfo = res.data.shortInfo || "";
                    this.name = res.data.name || "";
                    this.title = res.data.title || "";
                    this.money = res.data.money || "";
                    this.startDate = res.data.startDate || "";
                    this.completeDate = res.data.completeDate || "";
                    this.period = res.data.period || "";
                    this.projectInfo = res.data.projectInfo || "";
                    this.unitList = res.data.unitList || [];
                    this.postList = res.data.unitList || [];
                    this.lng = res.data.lng;
                    this.lat = res.data.lat;
                    this.fileList.forEach(i=>{
                        let obj ={
                            name:"",
                            url : i
                        }
                        this.imgList.push(obj)
                    })
                    this.isExtraFlag = false;
                } else {
                    this.$Message.warning(res.msg);
                }
            }).catch(error => {
                console.log(error)
            })
        },
        beforeIMgUpload(file) {
            const isLt2M = file.size / 1024 / 1024 < 1024;
            if (!isLt2M) {
                this.$Message.error("上传图片大小不能超过 2MB!");
            }
            return isLt2M;
        },
        reportlogImgSuccess(res) {
            if(res.code  == 200) {
                let obj = {
                    name:"",
                    url : res.data.url
                }
                this.imgList.push(obj);
            } else {
                this.$Message.warning(res.msg);
            }
            
        },
        exceedImg(files, fileList) {
            if (this.imgList.length == 10) {
                this.$Message.warning('上传图片最多10张');
                return;
            }
        },
        handleReportRemove(res, fileList) {
            this.imgList = fileList;
        },
        extraEdit() {
            this.corpCodeType = '1';
            this.isExtraFlag = true;
        },
        extraSub() {
            let unitList =[];
            let imgList = [];
            this.imgList.forEach(i=>{
                imgList.push(i.url)
            })
            this.unitList.forEach((v,i)=>{
                let obj= {
                    idx:i,
                    type:v.type,
                    name:v.name,
                }
                unitList.push(obj)
            })
            if(this.lng == ""){
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请选择位置"
                });
                return;
            }
            if(this.name == ""){
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请输入项目名称"
                });
                return;
            }
            if(this.title == ""){
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请输入项目简称"
                });
                return;
            }
            let params ={
                deptId:this.deptId,
                corpCodeType:this.corpCodeType,
                address:this.address,
                legalManPhone:this.legalManPhone,
                legalMan:this.legalMan,
                bizScope:this.bizScope,
                registerDate:this.registerDate,
                corpCode:this.corpCode,
                money:this.money,
                name:this.name,
                title:this.title,
                imgList:imgList,
                shortInfo:this.shortInfo,
                period:this.period,
                completeDate:this.completeDate,
                startDate:this.startDate,
                unitList:unitList,
                projectInfo:this.projectInfo,
                lng:this.lng,
                lat:this.lat,
            }
            getDeptExtraSet(params).then(res=>{
                if(res.code == 200) {
                    this.isExtraFlag = false;
                    this.$Message.success("操作成功");
                    this.getExtra(this.deptId);
                } else {
                    this.$Message.warning(res.msg);
                }
            }).catch(error=>{
                console.log(error)
            })
        },
    },
    mounted() {
    
    }
}
